import 'dart:ui';
import 'package:flame/sprite.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/painting.dart';

import 'ballGame.dart';


class BallTopDisplay {
  MusicButton musicButton;
  LevelDisplay levelDisplay;
  PauseButton pauseButton;
  final MyGame game;

  BallTopDisplay(this.game, int level) {
    musicButton = MusicButton(game);
    levelDisplay = LevelDisplay(game, level);
    pauseButton = PauseButton(game);
  }

  void updateLevel(int level) {
    levelDisplay.updateLevel(level);
  }

  void render(Canvas c) {
    levelDisplay.render(c);

    if (musicButton.isEnabled) {
      musicButton.enabledSprite.renderRect(c, musicButton.rect);
    } else {
      musicButton.disabledSprite.renderRect(c, musicButton.rect);
    }

    if (!pauseButton.isEnabled) {
      pauseTheGame = false;
      //暂停状态disabledSprite
      pauseButton.disabledSprite.renderRect(c, pauseButton.rect);
    } else {
      pauseTheGame = true;
      //执行状态
      pauseButton.enabledSprite.renderRect(c, pauseButton.rect);
    }
  }

  void onTapDown(TapDownDetails details) {
    // 音乐按钮
    if (musicButton.rect.contains(details.globalPosition)) {
      musicButton.onTapDown();
    }
    // 暂停/播放按钮
    if (pauseButton.rect.contains(details.globalPosition)) {
      pauseButton.onTapDown();
    }
  }
}

class LevelDisplay {
  final MyGame game;
  TextPainter painter;
  TextStyle textStyle;
  Offset position;
  int level;

  LevelDisplay(this.game, int level) {
    painter = TextPainter(
      textAlign: TextAlign.center,
      textDirection: TextDirection.ltr,
    );

    const Shadow shadow = const Shadow(
      blurRadius: 3,
      color: Color(0xff000000),
      offset: Offset.zero,
    );

    textStyle = const TextStyle(
      color: Color(0xffffffff),
      fontSize: 30,
      shadows: [shadow, shadow, shadow, shadow],
    );

    position = Offset.zero;

    updateLevel(level);
  }

  void updateLevel(int level) {
    painter.text = TextSpan(
      text: 'Lv. $level',
      style: textStyle,
    );

    painter.layout();

    position = Offset(
      game.screenSize.width - (game.tileSize * .25) - painter.width,
      game.tileSize * .25,
    );
  }

  void render(Canvas c) {
    painter.paint(c, position);
  }
}

class PauseButton {
  final MyGame game;
  Rect rect;
  Sprite enabledSprite;
  Sprite disabledSprite;
  bool isEnabled = false;

  PauseButton(this.game) {
    rect = Rect.fromLTWH(
      game.tileSize * .25,
      game.tileSize * .25,
      game.tileSize,
      game.tileSize,
    );
    enabledSprite = Sprite('files/ball/icon-play.png');
    disabledSprite = Sprite('files/ball/icon-pause.png');
  }

  void render(Canvas c) {
    if (isEnabled) {
      enabledSprite.renderRect(c, rect);
    } else {
      disabledSprite.renderRect(c, rect);
    }
  }

  void onTapDown() {
    isEnabled = !isEnabled;
//    if (isEnabled) {
//      isEnabled = false;
////      game.homeBGM.setVolume(0);
////      game.playingBGM.setVolume(0);
//    } else {
//      isEnabled = true;
////      game.homeBGM.setVolume(.25);
////      game.playingBGM.setVolume(.25);
//    }
  }
}

class MusicButton {
  final MyGame game;
  Rect rect;
  Sprite enabledSprite;
  Sprite disabledSprite;
  bool isEnabled = true;

  MusicButton(this.game) {
    rect = Rect.fromLTWH(
      game.tileSize * 1.5,
      game.tileSize * .25,
      game.tileSize,
      game.tileSize,
    );
    enabledSprite = Sprite('files/ball/icon-music-enabled.png');
    disabledSprite = Sprite('files/ball/icon-music-disabled.png');
  }

  void render(Canvas c) {
    if (isEnabled) {
      enabledSprite.renderRect(c, rect);
    } else {
      disabledSprite.renderRect(c, rect);
    }
  }

  void onTapDown() {
    if (isEnabled) {
      isEnabled = false;
//      game.homeBGM.setVolume(0);
//      game.playingBGM.setVolume(0);
    } else {
      isEnabled = true;
//      game.homeBGM.setVolume(.25);
//      game.playingBGM.setVolume(.25);
    }
  }
}
